package yhao.demo.service.erp.workflow.service.task;

import yhao.demo.service.erp.workflow.apilist.enums.task.TaskStatus;
import yhao.demo.service.erp.workflow.apilist.form.task.TaskDealRefuseForm;
import yhao.demo.service.erp.workflow.apilist.model.flow.FlowNodeModel;
import yhao.demo.service.erp.workflow.apilist.model.task.TaskModel;
import yhao.demo.service.erp.workflow.dao.TaskDao;
import yhao.demo.service.erp.workflow.service.pvm.ProcessSearcher;

import java.util.List;

public class RefuseTaskCommand extends TaskCommand {
    private TaskDealRefuseForm form;
    public RefuseTaskCommand(TaskDao taskDao, ProcessSearcher processSearcher, TaskDealRefuseForm form) {
        super(taskDao, processSearcher);
        this.form = form;
    }

    @Override
    protected void initContext() {
        initDealTask(form);
        List<FlowNodeModel> nodeList = processSearcher.findPreviousNodes(
                this.context.getTask().getProcessId(),this.context.getTask().getCurrentNodeId());
        FlowNodeModel nextNode = pickNextNode(form.getRefuseNodeId(), nodeList);
        this.context.setNextNode(nextNode);
    }

    @Override
    protected Boolean command() {
        TaskModel task = context.getTask();
        checkAssign(task,form);
        FlowNodeModel previousNode = context.getNextNode();
        task.setCurrentNodeId(previousNode.getId());
        task.setAssignType(previousNode.getAssignType());
        task.setAssignEntityId(previousNode.getAssignEntityId());
        task.setStatus(TaskStatus.RUNNING);
        if(initSubTask(task,previousNode)){
            task.setStatus(TaskStatus.SUB_TASK);
        }
        taskDao.taskGoToNext(task);
        updateTaskNodeItem(form.getItemList());
        addHistory(1,"[审核拒绝]","");
        addHistory(2,"[流向节点]",previousNode.getName());
        //初始化子任务
        return true;
    }
}
